﻿using UnityEngine;
using System.Collections;

public class ButtercupController : MonoBehaviour {

	public GameObject buttercupPrefab;
	public float spawnInterval;
	public float spawnIntervalMax;
	public float spawnIntervalMin;
	public float startTime;
	public float Xbound;
	public float Ybound;
	public bool inFront;
	public float Zposition;
	public float frontZposition;
	public float backZposition;
	public float chanceOfBeingBehind = 0.8f; 
	public float Xrotation;
	public float Yrotation;
	public float Zrotation;
	public Quaternion rotation;
	public Vector3 spawnPosition;

	public bool waitForSpawn = true;

	// Use this for initialization
	void Start () {
		spawnInterval = Random.Range(spawnIntervalMin, spawnIntervalMax);
		startTime = Time.time;
		inFront = GetRandomBoolean();
		if (inFront)
		{
			Zposition = frontZposition;
		} else
		{
			Zposition = backZposition;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (waitForSpawn && (startTime + spawnInterval < Time.time))
		{
			inFront = GetRandomBoolean();
			if (inFront)
			{
				Zposition = frontZposition;
			} else
			{
				Zposition = backZposition;
			}

			spawnPosition = new Vector3 (Random.Range(-Xbound, Xbound), Random.Range(-Ybound, Ybound), Zposition);
			rotation.eulerAngles = new Vector3(90, 0, 0);
			Instantiate (buttercupPrefab, spawnPosition, rotation);
			spawnInterval = Random.Range(spawnIntervalMin, spawnIntervalMax);
			startTime = Time.time;
		}
	}

	public bool GetRandomBoolean()
	{
		float rnd = Random.Range(0,1.0f);
		if (rnd < chanceOfBeingBehind)
		{
			return true;
		} else
		{
			return false;
		}
	}
}
